local benyu = fk.CreateSkill {

  name = "joy__benyu",

  tags = {  },

}



benyu:addEffect(fk.Damaged, {
  name = "joy__benyu",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(benyu.name) and data.from and not data.from.dead
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player,benyu.name) then
      local choices = {}
      if not player:isKongcheng() then
        table.insert(choices,"joy__benyu-damage::"..data.from.id)
      end
      local x = math.min(data.from:getHandcardNum(), 5)
      if player:getHandcardNum() < x  then
        table.insert(choices,"joy__benyu-drawcards:::"..x)
      end
      if #choices > 0 then
        local choice = room:askForChoice(player,choices,benyu.name,"#joy__benyu-ask")
        if choice == "joy__benyu-damage::"..data.from.id then
          local card = room:askForCard(player,1,1,false,benyu.name,true,".","#joy__benyu-damage::"..data.from.id)
          if #card > 0 then
            event:setCostData(self, {choice,card})
            return true
          end
        elseif choice == "joy__benyu-drawcards:::"..x then
            event:setCostData(self, {choice})
            return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self)[1] == "joy__benyu-damage::"..data.from.id then
      player.room:throwCard(event:getCostData(self)[2], benyu.name, player, player)
      player.room:damage{
        from = player,
        to = data.from,
        damage = 1,
        skillName = benyu.name,
      }
    else
      player:drawCards(math.min(5, data.from:getHandcardNum()) - player:getHandcardNum())
    end
  end,
})

return benyu